//处理博客前台的控制器
const clientService = require("../service/service.client");
class ClientController {
  //访问博客前台首页
  async viewIndex(ctx,next){
    //查询博客首页分类
    const categories = await clientService.getCategories();
    //查询首页最新博文列表数据
    const hotList = await clientService.getHotList();
    //获取最新博文列表数据
    const newList = await clientService.getNewList();
    const userInfo = ctx.session.userInfo;
    await ctx.render("client/index",{
      categories,
      hotList,
      newList,
      userInfo
    });
    await next();
  }
  //访问指定类目的文章列表
  async getListByCategory(ctx,next){
    //查询博客首页分类
    const categories = await clientService.getCategories();
    const list = await clientService.getListByCategory(ctx.params.id);
    const userInfo = ctx.session.userInfo;
    await ctx.render("client/list",{
      categories,
      list,
      userInfo
    });
    await next();
  }
 //访问搜索关键词得到的文章列表
  async getListBySearch(ctx,next){
    const categories = await clientService.getCategories();
    const list = await clientService.getListBySearch(ctx.query.keyword);
    const userInfo = ctx.session.userInfo;
    await ctx.render("client/search",{
      categories,
      list,
      keyword:ctx.query.keyword,
      userInfo
    })
    await next();
  }
  //获取文章详情
  async getDetail(ctx,next){
    const categories = await clientService.getCategories();
    const detail = await  clientService.getDetailById(+ctx.params.id);
    const userInfo = ctx.session.userInfo;
    await ctx.render("client/article",{
      categories,
      detail:detail[0][0],
      tabs:detail[1],
      prevArticle:detail[2][0],
      nextArticle:detail[3][0],
      userInfo
    });
    await next();
  }
  //访问登录页面
  async viewLogin(ctx,next){
    await ctx.render("client/login");
    await next();
  }
  //访问注册页面
  async viewRegister(ctx,next){
    await ctx.render("client/register");
    await next();
  }
  //注册用户
  async registerUser(ctx,next){
    //验证用户能不能注册
   const {username,password} = ctx.body;
   //去数据库查询,该用户名有没有被注册过
    const res = await clientService.verifyRegister(username);
    if(res.length){
      //该用户不能注册
      ctx.body = {
        code:1,
        status:401,
        message:"该用户名已经被被人注册了,请更换用户名"
      }
    }else{
      //可以注册
      const res = await clientService.registerUser(username,password);
      if(res.affectedRows){
        ctx.body = {
          code:0,
          status:200,
          message:"注册成功"
        }
      }
    }
   await next();
  }
  //登录
  async login(ctx,next){
    //先判断用户名或者密码是否正确
    const {username,password} = ctx.body;
    const result = await clientService.verifyLogin(username,password);
    if(result.length){
      //将用户登录信息存到session中去
      ctx.session.userInfo = result[0];
      //说明用户名和密码正确,可以登录
      ctx.body = {
        code:0,
        status:200,
        message:"登录成功"
      }
    }else{
      //说明用户名或者密码不对,登录失败
      ctx.body = {
        code:1,
        status:403,
        message:"用户名或者密码不正确,请重新登录"
      }
    }
    await next();
  }
  //退出登录
  async logout(ctx,next){
    //清除登录信息
    ctx.session.userInfo = null;
    ctx.redirect("/login");
    await next();
  }
}
module.exports = new ClientController();